﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for Utilities
/// </summary>
public class Utilities
{
    public Utilities()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    // Generic method for sending emails
    public static void SendMail(string from, string to, string subject,
    string body)
    {
        // Configure mail client (may need additional
        // code for authenticated SMTP servers)
        SmtpClient mailClient = new SmtpClient
        (PresentsConfiguration.MailServer);
        // Create the mail message
        MailMessage mailMessage = new MailMessage(from, to, subject, body);
        /*
        // For SMTP servers that require authentication
        message.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
        message.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendusername",
        "SmtpHostUserName");
        message.Fields.Add
        ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
        "SmtpHostPassword");
        */
        // Send mail
        mailClient.Send(mailMessage);
    }
    // Send error log mail
    public static void LogError(Exception ex)
    {
        // get the current date and time
        string dateTime = DateTime.Now.ToLongDateString() + ", at "
        + DateTime.Now.ToShortTimeString();
        // stores the error message
        string errorMessage = "Exception generated on " + dateTime;
        // obtain the page that generated the error
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        errorMessage += "\n\n Page location: " + context.Request.RawUrl;
        // build the error message
        errorMessage += "\n\n Message: " + ex.Message;
        errorMessage += "\n\n Source: " + ex.Source;
        errorMessage += "\n\n Method: " + ex.TargetSite;
        errorMessage += "\n\n Stack Trace: \n\n" + ex.StackTrace;
        // send error email in case the option is activated in Web.Config
        if (PresentsConfiguration.EnableErrorLogEmail)
        {
            string from = "noreply@cristiandarie.ro";
            string to = PresentsConfiguration.ErrorLogEmail;
            string subject = PresentsConfiguration.SiteName + " error report";
            string body = errorMessage;
            SendMail(from, to, subject, body);
        }
    }

    public static void TieButton(Page page, Control TextBoxToTie,
Control ButtonToTie)
    {
        // Init jscript
        string jsString = "";
        // Check button type and get required jscript
        if (ButtonToTie is LinkButton)
        {
            jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
            + page.ClientScript.GetPostBackEventReference
            (ButtonToTie, "").Replace(":", "$") + ";return false;} else return true;";
        }
        else if (ButtonToTie is ImageButton)
        {
            jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {"
            + page.ClientScript.GetPostBackEventReference
            (ButtonToTie, "").Replace(":", "$") + ";return false;} else return true;";
        }
        else
        {
            jsString = "if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {document."
            + "forms[0].elements['" + ButtonToTie.UniqueID.Replace
            (":", "_") + "'].click();return false;} else return true; ";
        }
        // Attach jscript to the onkeydown attribute—we have to cater for HtmlControl or WebControl
        if (TextBoxToTie is HtmlControl)
        {
            ((HtmlControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
        }
        else if (TextBoxToTie is WebControl)
        {
            ((WebControl)TextBoxToTie).Attributes.Add("onkeydown", jsString);
        }
    }
}